package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.domain.entity.NoticeLog;
import com.sikaryofficial.backend.mapper.NoticeLogMapper;
import com.sikaryofficial.backend.service.INoticeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 邮件发送日志Service业务层处理
 *
 * @author qinjinyuan
 * @date 2024-05-10
 */
@Service
public class NoticeLogServiceImpl extends ServiceImpl<NoticeLogMapper, NoticeLog> implements INoticeLogService {
    private static final Integer MAX_LENGTH = 1000;
    @Autowired
    private NoticeLogMapper noticeLogMapper;

    /**
     * 保存邮件发送日志
     *
     * @param costTime 发送耗时
     * @param content  发送内容
     * @param notifier 通知人（邮件地址或其他信息）
     * @param status   推送状态 0-成功 1-失败 默认0
     */
    @Override
    public void saveEmailLog(Long costTime, String content, String notifier, Integer status,String ip) {
        NoticeLog log = new NoticeLog();
        log.setCostTime(costTime);
        if (CharSequenceUtil.isNotBlank(content) && content.length() > MAX_LENGTH) {
            content = content.substring(0, MAX_LENGTH);
        }
        log.setContent(content);
        log.setCreatedBy(0L);
        log.setCreatedTime(new Date());
        log.setNotifier(notifier);
        log.setStatus(status);
        log.setRequestIp(ip);
        this.save(log);
    }
}
